package com.five.api.controller;

import com.five.controller.BaseController;
import com.five.api.service.OrdersService;
import com.five.entity.Orders;
import com.five.qo.PageQO;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 14:27
 * 订单 Controller层
 */
@Controller
@RequestMapping("orders")
@Api(tags = "订单业务接口")
public class OrdersController extends BaseController {

    @Autowired
    private OrdersService ordersService;

    @ResponseBody
    @RequestMapping(value = "selectOrdersById", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有订单/购买课程接口")
    public BaseResultVO<Orders> selectOrdersById(@ApiParam("用户id") @RequestParam("id") Long id){
        List<Orders> orders = ordersService.selectOrdersById(id);

        if (orders == null){
           return errorResp("当前用户无购买课程");
        }
           return successResp(orders);
    }

    @ResponseBody
    @RequestMapping(value = "selectOrdersByIdPage", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询订单/购买课程接口")
    public BaseResultVO<Orders> selectOrdersByIdPage(@ApiParam("分页对象") @Param("pageQO")  PageQO pageQO){
        System.out.println("订单号："+pageQO);
        List<Orders> orders = ordersService.selectOrdersByPage(pageQO);

        if (orders == null){
            return errorResp("当前用户无购买课程");
        }
        return successResp(orders);
    }

    @ResponseBody
    @RequestMapping(value = "insOrders",method = RequestMethod.POST)
    @ApiOperation("购买课程接口")
    public BaseResultVO insOrders(@ApiParam Long courseId, HttpServletRequest request){

        long l = ordersService.insOrder(courseId, request);
        return successResp(l);
    }
}
